//
//  CCUIView.m
//  CCFC_IPHONE
//
//  Created by  xuchen(陈旭)， xichen(陈曦)， qq：511272827 on 10-11-1.
//  Copyright 2010 cc_team. All rights reserved.
//
#include "CCAll.h"

#ifdef	OBJC_MACRO
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>		//可以使用view的layer属性的cornerRadius属性
#import "CCUIView.h"

//设置全屏幕的背景(不包括状态栏)
void setMainScrBgImg(UIView *mainScrView, NSString *imgName)
{
	UIImageView *imgView = CREATE_UIIMAGEVIEW(imgName);
	imgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
	[mainScrView addSubview:imgView];
}

// 设置UIView的背景图片 NB.可能背景会出现一条黑线
void setViewBgImg(UIView *view, NSString *imgName)
{
	setBkColorByImage(view, imgName);
}

// 设置view为圆角状态
void setViewRoundCorner(UIView *view, CGFloat radius)
{
	assert(view != nil);
	
	view.layer.cornerRadius = radius;
}

// 创建UILabel, 文本居中显示
UILabel *createLabel(const CGRect *rect, const NSString *title)
{
	assert(title != nil);
	
	UILabel *label = UI_ALLOC_CREATE(UILabel, rect->origin.x, rect->origin.y, 
											  rect->size.width, rect->size.height);

	label.text = (NSString *)title;
	label.textColor = COLOR_BLACK;
	label.backgroundColor = COLOR_WHITE;
	label.textAlignment = UITextAlignmentCenter;
	label.font = CREATE_FONT(COMMON_FONT_SIZE);
	[label autorelease];
	
	return label;
}

// 创建UIButton, 文本居中显示
UIButton *createButton(const CGRect *rect, const NSString *title)
{
	assert(rect != nil && title != nil);
	
	UIButton *btn = UI_ALLOC_CREATE(UIButton, rect->origin.x, rect->origin.y, rect->size.width, rect->size.height);

	SET_BUTTON_TITLE(btn, (NSString *)title);
	SET_BUTTON_TITLE_COLOR(btn, COLOR_BLACK);
	btn.backgroundColor = COLOR_WHITE;
	btn.titleLabel.font = CREATE_FONT(COMMON_FONT_SIZE);
	btn.titleLabel.textAlignment = UITextAlignmentCenter;
	
	return btn;
}

// 设置UIButton的背景图片
void setButtonBgImg(UIButton *btn, const NSString *normalBgImg, const NSString *selectedBgImg)
{
	UIImage *buttonImageNomal = CREATE_UIIMAGE((NSString *)normalBgImg);
	[btn setBackgroundImage:buttonImageNomal forState:UIControlStateNormal];

	UIImage *buttonImagePressed = CREATE_UIIMAGE((NSString *)selectedBgImg);
	[btn setBackgroundImage:buttonImagePressed forState:UIControlStateHighlighted];
}

// 以图片为填充背景  NB.可能背景会出现一条黑线
void setBkColorByImage(UIView *view, NSString *imgName)
{
	assert(view != nil && imgName != nil);
	
	view.backgroundColor = [[[UIColor alloc] initWithPatternImage:
							CREATE_UIIMAGE(imgName)] autorelease];
}

// 获取UIColor的RGBA的值
void getRGBAValue(const UIColor *color, 
				  CGFloat *red, CGFloat *green, CGFloat *blue, CGFloat *alpha)
{
	assert(color != nil);
	
	CGColorRef colorRef = [color CGColor];
	
	const CGFloat *tmComponents = CGColorGetComponents(colorRef);
	if(red)
	{
		*red = tmComponents[0];
	}
	if(green)
	{
		*green = tmComponents[1];
	}
	if(blue)
	{
		*blue = tmComponents[2];
	}
	if(alpha)
	{
		*alpha = tmComponents[3];
	}
}

CGFloat getRedValue(const UIColor *color)
{
	assert(color != nil);
	
	CGColorRef colorRef = [color CGColor];
	
	const CGFloat *tmComponents = CGColorGetComponents(colorRef);
	return tmComponents[0];
}

CGFloat getGreenValue(const UIColor *color)
{
	assert(color != nil);
	
	CGColorRef colorRef = [color CGColor];
	
	const CGFloat *tmComponents = CGColorGetComponents(colorRef);
	return tmComponents[1];
}

CGFloat getBlueValue(const UIColor *color)
{
	assert(color != nil);
	
	CGColorRef colorRef = [color CGColor];
	
	const CGFloat *tmComponents = CGColorGetComponents(colorRef);
	return tmComponents[2];
}

CGFloat getAlphaValue(const UIColor *color)
{
	assert(color != nil);
	
	CGColorRef colorRef = [color CGColor];
	
	const CGFloat *tmComponents = CGColorGetComponents(colorRef);
	return tmComponents[3];
}

// 由RGB的值获得CGColorRef指针
CGColorRef createCGColorRefByRGB(const CGFloat red, const CGFloat green, const CGFloat blue, const CGFloat alpha)
{
	CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
	const CGFloat rgbColor[] = {(float)red / 255, (float)green / 255 , (float) blue / 255, (float) alpha / 255};
	CGColorRef color = CGColorCreate(rgb, rgbColor);
	CGColorSpaceRelease(rgb);
	
	return color;
}

void releaseCGColorRef(CGColorRef colorRef)
{
	CGColorRelease(colorRef);
}

// 由UIColor获得CGColorRef指针
CGColorRef getCGColorRefByUIColor(const UIColor *color)
{
	assert(color != nil);
	
	return [color CGColor];
}

// 画弧形
void drawArc(CGContextRef ctx, CGFloat radius, CGFloat x, CGFloat y, 
			 CGFloat startArc, CGFloat endArc, int clockWise,
			 CGFloat red, CGFloat green, CGFloat blue, CGFloat alpha)
{	
	CGContextMoveToPoint(ctx, x, y);
	CGContextAddArc(ctx, x, y, radius, startArc, endArc, clockWise);
	
	CGContextSetRGBFillColor(ctx, red, green, blue, alpha);
	CGContextFillPath(ctx);
}

void drawArcByUIColor(CGContextRef ctx, CGFloat radius, CGFloat x, CGFloat y, 
			 CGFloat startArc, CGFloat endArc, int clockWise,
			 const UIColor *color)
{
	CGContextMoveToPoint(ctx, x, y);
	CGContextAddArc(ctx, x, y, radius, startArc, endArc, clockWise);
	
	CGContextSetRGBFillColor(ctx, 
							 getRedValue(color),
							 getGreenValue(color),
							 getBlueValue(color),
							 getAlphaValue(color));
	CGContextFillPath(ctx);
}


// 移除所有子view
void removeAllSubviews(UIView *view)
{
	assert(view != nil);
	
	NSArray *arr = view.subviews;
	for(UIView *temp in arr)
	{
		[temp removeFromSuperview];
	}
}

// 隐藏所有子view
void hideAllSubViews(const UIView *view)
{
	assert(view != nil);
	
	NSArray *arr = view.subviews;
	for(UIView *temp in arr)
	{
		temp.hidden = TRUE;
	}
}

// 创建一个指定区域大小的view
UIView *createView(const CGRect *rect)
{
	assert(rect != nil);
	
	UIView *view = UI_ALLOC_CREATE(UIView, 
								   rect->origin.x, rect->origin.y, 
								   rect->size.width, rect->size.height);
	return [view autorelease];
}

// 创建一个和指定view相同大小的view
UIView *createViewByView(const UIView *view)
{
	assert(view != nil);
	
	UIView *temp = UI_ALLOC_CREATE(UIView, 
								   GET_VIEW_X(view), GET_VIEW_Y(view), 
								   GET_VIEW_WIDTH(view), GET_VIEW_HEIGHT(view));
	return [temp autorelease];
}

// 创建一个指定区域大小的透明view
UIView *createTransparentView(const CGRect *rect)
{
	UIView *view = createView(rect);
	if(!view)
	{
		return nil;
	}
	view.backgroundColor = COLOR_CLEAR;
	
	return view;
}

// 创建一个和指定view相同大小的透明view
UIView *createTransparentViewByView(const UIView *view)
{
	UIView *temp = createViewByView(view);
	if(!temp)
	{
		return nil;
	}
	temp.backgroundColor = COLOR_CLEAR;
	
	return temp;
}

// 显示view
void showView(UIView *view)
{
	assert(view != nil);
	
	view.hidden = FALSE;
}

// 隐藏view
void hideView(UIView *view)
{
	assert(view != nil);
	
	view.hidden = TRUE;
}

// 根据字符串的字体大小获取字符串的像素长度
CGFloat getStrPixelLenByFontSize(NSString *str ,NSInteger fontSize)
{
	assert(str != nil && fontSize >= 0);
	
	CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:(CGFloat)fontSize]];
	return size.width;
}

// 根据字符串的字体大小获取字符串的像素高度
CGFloat getStrPixelHeightByFontSize(NSString *str ,NSInteger fontSize)
{
	assert(str != nil && fontSize >= 0);
	
	CGSize size = [str sizeWithFont:[UIFont systemFontOfSize:(CGFloat)fontSize]];
	return size.height;
}

// 获取字符串中字符下标从前到后，总长度小于等于指定像素长度的最大下标的值
NSInteger getMaxIndexLessEqualToLen(const NSString *str, const NSInteger fontSize, const int maxLen)
{
	assert(str != nil && fontSize >= 0 && maxLen >= 0);
	
	int sumLen = 0;
	for(NSInteger i = 0; i < str.length; i++)
	{
		NSString *temp = [str substringWithRange:NSMakeRange(i, 1)];	// 获取当前1个字符对应的字符串
		CGFloat currentCharPixelLen = getStrPixelLenByFontSize(temp, fontSize);
		
		sumLen += (NSInteger)currentCharPixelLen;		// 累计当前的总长度
		if(sumLen > maxLen)
		{
			return i - 1;
		}
		else if(sumLen == maxLen)
		{
			return i;
		}
		else 
		{
			continue;
		}
	}
	
	return str.length - 1;
}

// 获取UITextEffectsWindow的指针
UIWindow *getTextEffectsWindow()
{
	Class cls = NSClassFromString(@"UITextEffectsWindow");
	return [cls sharedTextEffectsWindow];
}

#endif	//OBJC_MACRO
